<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateVoucher{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateVoucherCode = new Zend_Validate();
        $validateDescription = new Zend_Validate();
        $validateOrderValue = new Zend_Validate();
        $validateValue = new Zend_Validate();
        $validateQuantity = new Zend_Validate();
        $validateTime = new Zend_Validate();
        
        $option = array('table' => 'vouchers', 'field' => 'voucher_code');
        
        if($edit){
            $option['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateVoucherCode->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 100)), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($option), true);
        
        $validateDescription->addValidator(new Zend_Validate_NotEmpty(), true);
        
        $validateOrderValue->addValidator(new Zend_Validate_NotEmpty(), true)
                            ->addValidator(new Zend_Validate_Digits(), true);
        
        $validateValue->addValidator(new Zend_Validate_NotEmpty(), true)
                        ->addValidator(new Zend_Validate_Digits(), true);
        
        $validateQuantity->addValidator(new Zend_Validate_NotEmpty(), true)
                        ->addValidator(new Zend_Validate_Digits(), true);
        
        $validateTime->addValidator(new Zend_Validate_NotEmpty(), true);
        
        /*
        * Check Voucher Code
        */
        
        if(!$validateVoucherCode->isValid($params['voucher_code'])){
            $this->_messageError['voucher_code'] = '<label class="control-label" for="inputError">' . $translate->_('voucher code') . current($validateVoucherCode->getMessages()) . '</label>';
            $this->_arrData['voucher_code'] = '';
            $this->_classError['voucher_code'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['voucher_code'] = $data['voucher_code'];
            }
        }else{
            $this->_messageError['voucher_code'] = '';
            $this->_arrData['voucher_code'] = $params['voucher_code'];
            $this->_classError['voucher_code'] = '';
        }
        
        /*
        * Check Description
        */
        
        if(!$validateDescription->isValid($params['description'])){
            $this->_messageError['description'] = '<label class="control-label" for="inputError">' . $translate->_('description') . current($validateDescription->getMessages()) . '</label>';
            $this->_arrData['description'] = '';
            $this->_classError['description'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['description'] = $data['description'];
            }
        }else{
            $this->_messageError['description'] = '';
            $this->_arrData['description'] = $params['description'];
            $this->_classError['description'] = '';
        }
        
        /*
        * Check Order Value
        */
        
        if(!$validateOrderValue->isValid($params['order_value'])){
            $this->_messageError['order_value'] = '<label class="control-label" for="inputError">' . $translate->_('order value') . current($validateOrderValue->getMessages()) . '</label>';
            $this->_arrData['order_value'] = '';
            $this->_classError['order_value'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['order_value'] = $data['order_value'];
            }
        }else{
            $this->_messageError['order_value'] = '';
            $this->_arrData['order_value'] = $params['order_value'];
            $this->_classError['order_value'] = '';
        }
        
        /*
        * Check Value
        */
        
        if(!$validateValue->isValid($params['value'])){
            $this->_messageError['value'] = '<label class="control-label" for="inputError">' . $translate->_('value') . current($validateValue->getMessages()) . '</label>';
            $this->_arrData['value'] = '';
            $this->_classError['value'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['value'] = $data['value'];
            }
        }else{
            $this->_messageError['value'] = '';
            $this->_arrData['value'] = $params['value'];
            $this->_classError['value'] = '';
        }
        
        /*
        * Check Quantity
        */
        
        if(!$validateQuantity->isValid($params['quantity'])){
            $this->_messageError['quantity'] = '<label class="control-label" for="inputError">' . $translate->_('value') . current($validateQuantity->getMessages()) . '</label>';
            $this->_arrData['quantity'] = '';
            $this->_classError['quantity'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['quantity'] = $data['quantity'];
            }
        }else{
            $this->_messageError['quantity'] = '';
            $this->_arrData['quantity'] = $params['quantity'];
            $this->_classError['quantity'] = '';
        }
        
        /*
        * Check Time
        */
        
        if(!$validateTime->isValid($params['time'])){
            $this->_messageError['time'] = '<label class="control-label" for="inputError">' . $translate->_('time') . current($validateTime->getMessages()) . '</label>';
            $this->_arrData['time'] = '';
            $this->_classError['time'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['time'] = $data['time'];
            }
        }else{
            $this->_messageError['time'] = '';
            $this->_arrData['time'] = $params['time'];
            $this->_classError['time'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        
        $type = false;
            
        if($params['type'] == 1){
            $type = true;
        }
        
        $this->_arrData['type'] = $type;
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        $info = new Zenddn_System_Info();
        $user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            $this->_arrData['created_by'] = $user_id;
        }
        
        if(!$this->isError()){
            $arrTime = explode(" - ", $params['time']);
            $this->_arrData['start_date'] = date("Y-m-d", strtotime($arrTime[0]));
            $this->_arrData['end_date'] = date("Y-m-d", strtotime($arrTime[1]));
            unset($this->_arrData['time']);
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}